<?php


namespace app\common\library;


use hiredis;


Class StatModel
{

    function user_scan_incr($city_id, $invalid)
    {
        $key = "DAILY_USR_SCAN_TDCODE|" . $city_id;
        hiredis::db()->hIncrBy($key, date('Y-m-d'), 1);
        if ($invalid) {
            $key = "DAILY_USR_SCAN_TDCODE_INVALID|" . $city_id;
            hiredis::db()->hIncrBy($key, date('Y-m-d'), 1);
        }
    }

    function user_count_incr($city_id)
    {
        $key = "DAILY_USR_GROWTH|" . $city_id;
        hiredis::db()->hIncrBy($key, date('Y-m-d'), 1);
        hiredis::db()->hIncrBy($key, 'total', 1);
    }

    function user_count_total($city_id)
    {
        $key = "DAILY_USR_GROWTH|" . $city_id;
        return intval(hiredis::db()->hGet($key, 'total'));
    }

    function user_count_tomonth_sum($city_id)
    {
        $key = "DAILY_USR_GROWTH|" . $city_id;
        $fields = $this->tomonth_dates();

        $vals = hiredis::db()->hMget($key, $fields);
        $sum = 0;
        foreach ($vals as $k => $v) {
            $sum += intval($v);
        }
        return $sum;
    }

    function daily_user_growth_list($city_id, $start_date, $end_date)
    {
        if (empty($end_date)) {
            $end_date = date('Y-m-d');
        }
        $end_timestamp = strtotime($end_date);
        $days = 30;
        if (!empty($start_date)) {
            $t = $end_timestamp - strtotime($start_date);
            $days = intval($t / 3600 / 24);
            if ($days > 30) {
                $days = 30;
            }
        }

        $fields = [];
        for ($i = 0; $i < $days; $i++) {
            $fields[] = date('Y-m-d', $end_timestamp - $i * 3600 * 24);
        }


        $key = "DAILY_USR_GROWTH|" . $city_id;

        $vals = hiredis::db()->hMget($key, $fields);
        $ret = [];
        if (is_array($vals)){
            foreach ($vals as $k => $v) {
                $ret[$k] = intval($v);
            }
        }

        return $ret;
    }

    function user_order_incr($city_id,$svca_id)
    {
        $key = "DAILY_USR_ORDER_GROWTH|" . $city_id;
        hiredis::db()->hIncrBy($key, date('Y-m-d'), 1);
        hiredis::db()->hIncrBy($key, 'total', 1);


        if(!empty($svca_id))
        {
            //记录区域
            $key_svca = "DAILY_USR_ORDER_GROWTH|" . $svca_id;
            hiredis::db()->hIncrBy($key_svca, date('Y-m-d'), 1);
            hiredis::db()->hIncrBy($key_svca, 'total', 1);
        }
    }

    function user_order_count_total($city_id)
    {
        $key = "DAILY_USR_ORDER_GROWTH|" . $city_id;
        return intval(hiredis::db()->hGet($key, 'total'));
    }

    function user_ride_count_tomonth_sum($city_id)
    {
        $key = "DAILY_USR_ORDER_GROWTH|" . $city_id;
        $fields = $this->tomonth_dates();

        $vals = hiredis::db()->hMget($key, $fields);
        $sum = 0;
        foreach ($vals as $k => $v) {
            $sum += intval($v);
        }
        return $sum;
    }

    function user_ride_count($city_id, $date,$svca_id = null)
    {
        if ($svca_id == null){
            $key = "DAILY_USR_ORDER_GROWTH|" . $city_id;
            $v = hiredis::db()->hGet($key, $date);
            return intval($v);
        }
        else{
            $key = "DAILY_USR_ORDER_GROWTH|" . $svca_id;
            $v = hiredis::db()->hGet($key, $date);
            return intval($v);
        }

    }

    function user_ride_count_sv($city_id, $date)
    {
        $key = "DAILY_USR_ORDER_GROWTH|" . $city_id;
        $v = hiredis::db()->hGet($key, $date);
        return intval($v);
    }

    function daily_user_order_growth_list($city_id, $start_date, $end_date)
    {
        if (empty($end_date)) {
            $end_date = date('Y-m-d');
        }
        $end_timestamp = strtotime($end_date);
        $days = 30;
        if (!empty($start_date)) {
            $t = $end_timestamp - strtotime($start_date);
            $days = intval($t / 3600 / 24);
            if ($days > 30) {
                $days = 30;
            }
        }

        $fields = [];
        for ($i = 0; $i < $days; $i++) {
            $fields[] = date('Y-m-d', $end_timestamp - $i * 3600 * 24);
        }

        $key = "DAILY_USR_ORDER_GROWTH|" . $city_id;
        $vals = hiredis::db()->hMget($key, $fields);
        $ret = [];
        if (is_array($vals)){
            foreach ($vals as $k => $v) {
                $ret[$k] = intval($v);
            }
        }

        return $ret;
    }

    function daily_user_scan_qdcode_list($city_id, $start_date, $end_date)
    {
        if (empty($end_date)) {
            $end_date = date('Y-m-d');
        }
        $end_timestamp = strtotime($end_date);
        $days = 30;
        if (!empty($start_date)) {
            $t = $end_timestamp - strtotime($start_date);
            $days = intval($t / 3600 / 24);
            if ($days > 30) {
                $days = 30;
            }
        }

        $fields = [];
        for ($i = 0; $i < $days; $i++) {
            $fields[] = date('Y-m-d', $end_timestamp - $i * 3600 * 24);
        }

        $key = "DAILY_USR_SCAN_TDCODE|" . $city_id;

        $vals = hiredis::db()->hMget($key, $fields);
        $ret = [];
        if (is_array($vals)){
            foreach ($vals as $k => $v) {
                $ret[$k] = intval($v);
            }
        }

        return $ret;
    }

    function daily_user_scan_qdcode_invalid_list($city_id, $start_date, $end_date)
    {
        if (empty($end_date)) {
            $end_date = date('Y-m-d');
        }
        $end_timestamp = strtotime($end_date);
        $days = 30;
        if (!empty($start_date)) {
            $t = $end_timestamp - strtotime($start_date);
            $days = intval($t / 3600 / 24);
            if ($days > 30) {
                $days = 30;
            }
        }
        $fields = [];
        for ($i = 0; $i < $days; $i++) {
            $fields[] = date('Y-m-d', $end_timestamp - $i * 3600 * 24);
        }

        $key = "DAILY_USR_SCAN_TDCODE_INVALID|" . $city_id;

        $vals = hiredis::db()->hMget($key, $fields);
        $ret = [];
        if (is_array($vals)){
            foreach ($vals as $k => $v) {
                $ret[$k] = intval($v);
            }
        }

        return $ret;
    }

    function tomonth_dates()
    {
        $m = date('Y-m-');
        $d = intval(date('d'));
        $arr = [];
        for ($i = 1; $i <= $d; $i++) {
            $arr[] = $m . str_pad($i, 2, '0', STR_PAD_LEFT);
        }
        return $arr;
    }

    function user_ride_order_amount_incr($city_id, $date, $incr,$svca_id)
    {
        $d = substr($date, 0, 10);
        $key = "DAILY_USR_RIDE_ORDER_AMOUNT_GROWTH|" . $city_id;
        hiredis::db()->hIncrBy($key, $d, $incr);
        hiredis::db()->hIncrBy($key, 'total', $incr);


        if(!empty($svca_id))
        {
            //记录区域
            $key_svca = "DAILY_USR_RIDE_ORDER_AMOUNT_GROWTH|" . $svca_id;
            hiredis::db()->hIncrBy($key_svca, $d, $incr);
            hiredis::db()->hIncrBy($key_svca, 'total', $incr);
        }
    }

    function user_ride_amount($city_id, $date,$svca_id = null)
    {
        if ($svca_id == null){
            $key = "DAILY_USR_RIDE_ORDER_AMOUNT_GROWTH|" . $city_id;
            $v = hiredis::db()->hGet($key, $date);
            return intval($v);
        }
        else {
            $key = "DAILY_USR_RIDE_ORDER_AMOUNT_GROWTH|" . $svca_id;
            $v = hiredis::db()->hGet($key, $date);
            return intval($v);
        }
    }

    function user_ride_order_amount_total($city_id)
    {
        $key = "DAILY_USR_RIDE_ORDER_AMOUNT_GROWTH|" . $city_id;

        return intval(hiredis::db()->hGet($key, 'total'));
    }

    function user_ride_order_amount_tomonth_sum($city_id)
    {
        $key = "DAILY_USR_RIDE_ORDER_AMOUNT_GROWTH|" . $city_id;
        $fields = $this->tomonth_dates();

        $vals = hiredis::db()->hMget($key, $fields);
        $sum = 0;
        foreach ($vals as $k => $v) {
            $sum += intval($v);
        }
        return $sum;
    }

    function user_ride_order_amount_weekly($city_id, $start_date, $end_date)
    {
        if (empty($end_date)) {
            $end_date = date('Y-m-d');
        }
        $end_timestamp = strtotime($end_date);
        $days = 30;
        if (!empty($start_date)) {
            $t = $end_timestamp - strtotime($start_date);
            $days = intval($t / 3600 / 24);
            if ($days > 30) {
                $days = 30;
            }
        }
        $fields = [];
        for ($i = 0; $i < $days; $i++) {
            $fields[] = date('Y-m-d', $end_timestamp - $i * 3600 * 24);
        }

        $key = "DAILY_USR_RIDE_ORDER_AMOUNT_GROWTH|" . $city_id;
        $vals = hiredis::db()->hMget($key, $fields);
        $ret = [];
        if (is_array($vals)){
            foreach ($vals as $k => $v) {
                $ret[$k] = intval($v);
            }
        }

        return $ret;
    }

    function user_charge_order_amount_weekly($city_id, $start_date = null, $end_date)
    {
        if (empty($end_date)) {
            $end_date = date('Y-m-d');
        }
        $end_timestamp = strtotime($end_date);
        $days = 30;
        if (!empty($start_date)) {
            $t = $end_timestamp - strtotime($start_date);
            $days = intval($t / 3600 / 24);
            if ($days > 30) {
                $days = 30;
            }
        }
        $fields = [];
        for ($i = 0; $i < $days; $i++) {
            $fields[] = date('Y-m-d', $end_timestamp - $i * 3600 * 24);
        }

        $key = "DAILY_USR_CHARGE_ORDER_AMOUNT_GROWTH|" . $city_id;

        $vals = hiredis::db()->hMget($key, $fields);
        $ret = [];
        if (is_array($vals)){
            foreach ($vals as $k => $v) {
                $ret[$k] = intval($v);
            }
        }

        return $ret;
    }

    function user_charge_order_amount_incr($city_id, $incr)
    {
        $key = "DAILY_USR_CHARGE_ORDER_AMOUNT_GROWTH|" . $city_id;
        hiredis::db()->hIncrBy($key, date('Y-m-d'), $incr);
        hiredis::db()->hIncrBy($key, 'total', $incr);
    }

    function user_charge_order_amount_total($city_id)
    {
        $key = "DAILY_USR_CHARGE_ORDER_AMOUNT_GROWTH|" . $city_id;
        return intval(hiredis::db()->hGet($key, 'total'));
    }

    function user_charge_order_amount_tomonth_sum($city_id)
    {
        $key = "DAILY_USR_CHARGE_ORDER_AMOUNT_GROWTH|" . $city_id;
        $fields = $this->tomonth_dates();

        $vals = hiredis::db()->hMget($key, $fields);
        $sum = 0;
        foreach ($vals as $k => $v) {
            $sum += intval($v);
        }
        return $sum;
    }

    function user_membership_order_amount_incr($city_id, $incr)
    {
        $key = "DAILY_MEMBERSHIP_CHARGE_ORDER_AMOUNT_GROWTH|" . $city_id;
        hiredis::db()->hIncrBy($key, date('Y-m-d'), $incr);
        hiredis::db()->hIncrBy($key, 'total', $incr);
    }

    function user_membership_order_amount_total($city_id)
    {
        $key = "DAILY_MEMBERSHIP_CHARGE_ORDER_AMOUNT_GROWTH|" . $city_id;

        return intval(hiredis::db()->hGet($key, 'total'));
    }

    function user_membership_order_amount_tomonth_sum($city_id)
    {
        $key = "DAILY_MEMBERSHIP_CHARGE_ORDER_AMOUNT_GROWTH|" . $city_id;
        $fields = $this->tomonth_dates();

        $vals = hiredis::db()->hMget($key, $fields);
        $sum = 0;
        foreach ($vals as $k => $v) {
            $sum += intval($v);
        }
        return $sum;
    }


    function user_ridecard_order_amount_incr($city_id, $incr)
    {
        $key = "DAILY_RIDECARD_CHARGE_ORDER_AMOUNT_GROWTH|" . $city_id;
        hiredis::db()->hIncrBy($key, date('Y-m-d'), $incr);
        hiredis::db()->hIncrBy($key, 'total', $incr);
    }

    function user_ridecard_order_amount_total($city_id)
    {
        $key = "DAILY_RIDECARD_CHARGE_ORDER_AMOUNT_GROWTH|" . $city_id;

        return intval(hiredis::db()->hGet($key, 'total'));
    }

    function user_ridecard_order_amount_tomonth_sum($city_id)
    {
        $key = "DAILY_RIDECARD_CHARGE_ORDER_AMOUNT_GROWTH|" . $city_id;
        $fields = $this->tomonth_dates();

        $vals = hiredis::db()->hMget($key, $fields);
        $sum = 0;
        foreach ($vals as $k => $v) {
            $sum += intval($v);
        }
        return $sum;
    }

    function user_refund_order_amount_incr($city_id, $incr)
    {
        $key = "DAILY_USR_REFUND_ORDER_AMOUNT_GROWTH|" . $city_id;
        hiredis::db()->hIncrBy($key, date('Y-m-d'), $incr);
        hiredis::db()->hIncrBy($key, 'total', $incr);
    }

    function user_refund_order_amount_total($city_id)
    {
        $key = "DAILY_USR_REFUND_ORDER_AMOUNT_GROWTH|" . $city_id;

        return intval(hiredis::db()->hGet($key, 'total'));
    }

    function user_refund_order_amount_tomonth_sum($city_id)
    {
        $key = "DAILY_USR_REFUND_ORDER_AMOUNT_GROWTH|" . $city_id;
        $fields = $this->tomonth_dates();

        $vals = hiredis::db()->hMget($key, $fields);
        $sum = 0;
        foreach ($vals as $k => $v) {
            $sum += intval($v);
        }
        return $sum;
    }

    function bike_count_set($city_id, $num)
    {
        $key = "BIKE_COUNT|" . $city_id;
        hiredis::db()->hSet($key, date('Y-m-d'), $num);
    }
    function bike_count_get($city_id, $date_time)
    {
        $key = "BIKE_COUNT|" . $city_id;
        $val = hiredis::db()->hGet($key, $date_time);

        return $val;
    }

    function bike_count_weekly($city_id, $start_date = null, $end_date = null)
    {
        if (empty($end_date)) {
            $end_date = date('Y-m-d');
        }
        $end_timestamp = strtotime($end_date);
        $days = 30;
        if (!empty($start_date)) {
            $t = $end_timestamp - strtotime($start_date);
            $days = intval($t / 3600 / 24);
            if ($days > 30) {
                $days = 30;
            }
        }
        $fields = [];
        for ($i = 0; $i < $days; $i++) {
            $fields[] = date('Y-m-d', $end_timestamp - $i * 3600 * 24);
        }

        $key = "BIKE_COUNT|" . $city_id;
        $vals = hiredis::db()->hMget($key, $fields);
        $ret = [];
        if (is_array($vals)){
            foreach ($vals as $k => $v) {
                $ret[$k] = intval($v);
            }
        }

        return $ret;
    }

    //设置天气
    function analyze_setwealth($city_id,$city_name,$wealth)
    {
        $end_date = date('Y-m-d');

        $key = "DAILY_WEALTH|" . $city_id;
        hiredis::db()->hSet($key, date('Y-m-d'), $wealth);
    }

    function analyze_wealth($city_id, $start_date, $end_date)
    {
        if (empty($end_date)) {
            $end_date = date('Y-m-d');
        }
        $end_timestamp = strtotime($end_date);
        $days = 30;
        if (!empty($start_date)) {
            $t = $end_timestamp - strtotime($start_date);
            $days = intval($t / 3600 / 24);
            if ($days > 30) {
                $days = 30;
            }
        }
        $fields = [];
        for ($i = 0; $i < $days; $i++) {
            $fields[] = date('Y-m-d', $end_timestamp - $i * 3600 * 24);
        }

        $key = "DAILY_WEALTH|" . $city_id;
        $vals = hiredis::db()->hMget($key, $fields);
        $ret = [];
        if (is_array($vals)){
            foreach ($vals as $k => $v) {
                $ret[$k] = $v;
            }
        }
        return $ret;
    }


    //换电，挪车
    function ops_month_repark_incr($city_id,$name,$count)
    {
        $key = "MONTH_REPARKRANK|" . $city_id . date('Y-m');

        hiredis::db()->zAdd($key, $count, $name);
    }

    function ops_month_repark_rank($city_id,$num)
    {
        $key = "MONTH_REPARKRANK|" . $city_id . date('Y-m');

        return hiredis::db()->zRange($key, 0,$num, true);
    }

    function ops_month_changebattery_incr($city_id,$name,$count)
    {
        $key = "MONTH_CHANGEBATTERYRANK|" . $city_id . date('Y-m');

        hiredis::db()->zAdd($key, $count, $name);
    }

    function ops_month_changebattery_rank($city_id,$num)
    {
        $key = "MONTH_CHANGEBATTERYRANK|" . $city_id . date('Y-m');

        return hiredis::db()->zRange($key, 0,$num, true);
    }

    //日报
    function daily_report($city_id,$date,$arr_fields)
    {
        $key = "DAILY_REPORT|" . $city_id;

        hiredis::db()->hSet($key,$date,json_encode($arr_fields));
    }

    function daily_person_report($city_id,$date,$arr_fields)
    {
        $key = "DAILY_PERSON_REPORT|" . $city_id;

        hiredis::db()->hSet($key,$date,json_encode($arr_fields));
    }

    function get_daily_report($city_id, $start_date = null, $end_date = null)
    {
        if (empty($end_date)) {
            $end_date = date('Y-m-d');
        }

        $end_timestamp = strtotime($end_date);
        $days = 30;
        if (!empty($start_date)) {
            $t = $end_timestamp - strtotime($start_date);
            $days = intval($t / 3600 / 24);
            if ($days > 30) {
                $days = 30;
            }
        }
        $fields = [];
        if ($days == 0)
        {
            $fields[] = date('Y-m-d', strtotime($start_date));
        }
        else
        {
            for ($i = 0; $i < $days; $i++) {
                $fields[] = date('Y-m-d', $end_timestamp - $i * 3600 * 24);
            }
        }


        $key = "DAILY_REPORT|" . $city_id;
        $vals = hiredis::db()->hMget($key, $fields);
        $ret = [];
        if (is_array($vals)){
            foreach ($vals as $k => $v) {
                if (is_bool($v))
                {
                    continue;
                }
                else
                {
                    $v['day_time'] = $k;
                    $ret[] = $v;
                }

            }
        }

        return $ret;
    }

    function get_daily_person_report($city_id,$date)
    {
        $key = "DAILY_PERSON_REPORT|" . $city_id;

        return hiredis::db()->hGet($key,$date);
    }


    //获取分账错误
    function incr_profit_error($order_id)
    {
        $key = "PROFIT|" . date('Y-m-d');
        hiredis::db()->hIncrBy($key, $order_id, 1);
    }

    function get_profit_error($order_id)
    {
        $key = "PROFIT|" . date('Y-m-d');
        return intval(hiredis::db()->hGet($key, $order_id));
    }
}
